var productName; /*要增加的产品分组名称*/
var parentObj; /*增加产品分组的当前对象*/
var $modify; /*修改项当前对象*/
var groupObj; /*修改分组当前组里面的P*/
var newGroupName; /*修改分组新名字*/

$(function() {
	ForLiWidth();
	/*分类菜单滑动效果*/
	var flag = false;
	$(".z-large-classify p").click(function() {
		if (!$(this).parent().find(".folder").is(":animated")) {
			$(this).parent().find(".folder").slideDown();
			$(this).parent().find(".fa-sort-desc").addClass("fa-rotate-180");
			flag = true;
			$(this).parent().siblings().find(".folder").slideUp();
			$(this).parent().siblings().find(".fa-sort-desc").removeClass("fa-rotate-180");
		}
		if ((!$(this).parent().find(".folder").is(":animated")) && flag) {
			$(this).parent().find(".folder").slideUp();
			$(this).parent().find(".fa-sort-desc").removeClass("fa-rotate-180");
		}
	});

	/*竖滚动条*/
	$(".folder>ul,.scoll").mCustomScrollbar({
		theme: "dark-2",
		scrollButtons: {
			enable: true
		},
		autoHideScrollbar: false,
		scrollInertia: 200,
		horizontalScroll: false,
	});
	/*横滚动条*/
	//	$(".infeed-scorll").mCustomScrollbar({
	//		theme: "dark-2",
	//		scrollButtons: {
	//			enable: false
	//		},
	//		autoHideScrollbar: true,
	//		scrollInertia: 200,
	//		horizontalScroll: true,
	//	});

	/*添加产品*/
	$(".fa-plus-circle ").click(function() {
		appendProduct($(this))
	});

	function appendProduct(plus) {
		parentObj = plus.parent();
		$("#add").trigger("click");
	}

	$(".modal-footer #add-confirm").click(function() {
		productName = $(".add-product").val();
		if (productName == "") {
			$(this).parent().parent().find(".modal-body").find(".add-product").focus();
			return false;
		} else {
			if (parentObj.attr("class") == "Third") {
				var $li = $("<li><p class='group-name'>" + productName + "</p><span><i class='fa fa-trash fa-lg help right-margin'></i><i class='fa fa-pencil-square fa-lg help'></i></span></li>");
				$li.hover(function() {
					ThirdIn($(this));
				}, function() {
					ThirdOut($(this));
				});
				if (parentObj.find("li").length == '0') {
					parentObj.find(">span").before($li);
				} else {
					var allLi = parentObj.find("li");
					allLi.eq(allLi.length - 1).after($li);
				}
			} else {
				var $secondLi = $("<li><div class='second-menu'><i class='fa fa-caret-down fa-lg top-margin'></i><p class='group-name'>" + productName + "</p><span class='second-span'><i class='fa fa-trash fa-lg help right-margin'></i><i class='fa fa-pencil-square fa-lg help'></i></span></div><ul class='Third'><span class='fa fa-plus-circle fa-lg help'></span></ul></li>");

				$secondLi.find(".fa-plus-circle").click(function() {
					appendProduct($(this))
				});
				$secondLi.find(".second-menu").click(function() {
					thirdSlide($(this))
				});
				$secondLi.hover(function() {
					LiHoverIn($(this).find(".second-menu"))
				}, function() {
					LiHoverOut($(this).find(".second-menu"))
				});
				parentObj.find(">span").before($secondLi);
				//					alert(parentObj.html());				
			}
		}
	});

	/*二级删除&修改产品名称*/
	$(".z-detail-classify li .second-menu").hover(function() {
		LiHoverIn($(this))
	}, function() {
		LiHoverOut($(this))
	});
	/*三级删除&修改产品*/
	$(".z-detail-classify li .Third li").hover(function() {
		ThirdIn($(this))
	}, function() {
		ThirdOut($(this))
	});
	/*确认修改产品*/
	$(".modal-footer #modify-confirm").click(function() {
		newGroupName = $(".modify-product").val();
		if (newGroupName == "") {
			$(this).parent().parent().find(".modal-body").find(".modify-product").focus();
			return false;
		} else {
			//			$("title").html(newGroupName);
			groupObj.html(newGroupName);
		}
	});

	/*三级菜单下滑*/
	$(".second-menu").click(function() {
		thirdSlide($(this));
	});

	/*灯箱效果*/
	$(".catalogue").tabs({
		fx: {
			opacity: "toggle",
			height: "fadeOut"
		},
		event: "mousemove"
	});
	$(".catalogue .detail-classify li").each(function() {
		$(this).find("a").hover(function() {
			$(this).addClass("detail-active");
			$(this).parent().siblings().find("a").removeClass("detail-active");
		});
	});
})

/*二级删改图标出现*/
function LiHoverIn(oneLi) {
	var $delet = oneLi.parent().find(".second-span").find(".fa-trash");
	$modify = oneLi.parent().find(".second-span").find(".fa-pencil-square");
	oneLi.parent().find(".second-span").fadeIn(0);
	$delet.click(function() {
		oneLi.parent().remove();
	});
	$modify.unbind();
	$modify.click(function() {
		groupObj = $modify.parent().parent().parent().find(".group-name");
		$("#revise").trigger("click");
	});
}

function LiHoverOut(oneLi) {
		oneLi.parent().find(".second-span").fadeOut(0);
	}
	/*三级菜单下滑*/

function thirdSlide(second) {
		if (!second.parent().find(".Third").is(":animated")) {
			second.parent().find(".Third").slideDown();
			second.find(".fa-caret-down").addClass("fa-rotate-180");
			flag = true;
			second.parent().siblings().find(".Third").slideUp();
			second.parent().siblings().find(".fa-caret-down").removeClass("fa-rotate-180");
		}
		if ((!second.parent().find(".Third").is(":animated")) && flag) {
			second.parent().find(".Third").slideUp();
			second.find(".fa-caret-down").removeClass("fa-rotate-180");
		}
	}
	/*三级图标删改*/

function ThirdIn(oneLi) {
	var $delet = oneLi.find("span").find(".fa-trash");
	$modify = oneLi.find("span").find(".fa-pencil-square");
	oneLi.find("span").fadeIn(0);
	$delet.click(function() {
		oneLi.remove();
	});
	$modify.unbind();
	$modify.click(function() {
		groupObj = oneLi.find(".group-name");
		$("#revise").trigger("click");
	});
}

function ThirdOut(oneLi) {
	oneLi.find("span").fadeOut(0);
}

/*判断p的字数*/
function ForLiWidth() {
	$(".folder .z-detail-classify li").hover(function() {
		if ($(this).find("p").html().length > 6) {
			var txt = $(this).find("p").html();
			var $tip = $("<p class='tip-p' style='width:200px;backgroud-color:#FFFFFF;position:absolute;display:none;z-index:99999'></p>").text(txt);
			$("body").append($tip);
			var w1 = Math.round($(this).offset().left); //li离网页左边的距离
			var h1 = Math.round($(this).offset().top); //li离网页顶部的距离
			var w3 = $(this).width(); //li的宽度
			var h2 = $tip.outerHeight(true); //提示框的高度
			//			$("title").html(h2);
			var leftDis = w1 + w3 / 2 - 100;
			var topDis = h1 - h2;
			$tip.css({
				"left": leftDis + "px",
				"top": topDis + "px"
			})
			$tip.stop(true, true);
			$tip.fadeIn(0);
		}
	}, function() {
		$(".tip-p").fadeOut(0);
	});
}


/*目录树部分*/
$(function() {
	var zTree;
	var demoIframe;
	var setting = {
		view: {
			dblClickExpand: false,
			showLine: true,
			selectedMulti: false
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pId",
				rootPId: ""
			}
		},
		//	callback: {
		//		beforeClick: function(treeId, treeNode) {
		//			var zTree = $.fn.zTree.getZTreeObj("tree");
		//			if (treeNode.isParent) {
		//				zTree.expandNode(treeNode);
		//				return false;
		//			} else {
		//				demoIframe.attr("src", treeNode.file + ".html");
		//				return true;
		//			}
		//		}
		//	}
	};
	var zNodes = [{
		id: 1,
		pId: 0,
		name: "健康美容",
		open: true
	}, {
		id: 2,
		pId: 0,
		name: "安全与防护",
		open: false
	}, {
		id: 201,
		pId: 2,
		name: "化工材料",
	}, {
		id: 206,
		pId: 2,
		name: "能源设备",
	}, {
		id: 3,
		pId: 0,
		name: "鞋包&配饰",
		open: false
	}, {
		id: 301,
		pId: 3,
		name: "鞋子",
	}, {
		id: 302,
		pId: 3,
		name: "鞋子",
	}, {
		id: 303,
		pId: 3,
		name: "鞋子",
	}, {
		id: 304,
		pId: 3,
		name: "鞋子",
	}, {
		id: 4,
		pId: 0,
		name: "家具",
	}, {
		id: 401,
		pId: 4,
		name: "灯具",
	}, {
		id: 5,
		pId: 0,
		name: "汽配",
		open: false
	}, {
		id: 6,
		pId: 0,
		name: "车轴",
		open: false
	}, {
		id: 601,
		pId: 6,
		name: "轮子",
	}, {
		id: 602,
		pId: 6,
		name: "盒子",
	}, {
		id: 603,
		pId: 6,
		name: "其他",
	}];
	$(document).ready(function() {
		var t = $("#tree");
		t = $.fn.zTree.init(t, setting, zNodes);
		demoIframe = $("#testIframe");
		demoIframe.bind("load", loadReady);
		var zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.selectNode(zTree.getNodeByParam("id", 101));
	});

	function loadReady() {
		var bodyH = demoIframe.contents().find("body").get(0).scrollHeight,
			htmlH = demoIframe.contents().find("html").get(0).scrollHeight,
			maxH = Math.max(bodyH, htmlH),
			minH = Math.min(bodyH, htmlH),
			h = demoIframe.height() >= maxH ? minH : maxH;
		if (h < 530) h = 530;
		demoIframe.height(h);
	}

	/*tabs-3查看被过滤的词*/
	$(".check-filter-word").click(function() {
		$("#filter-btn").trigger("click");
	});
	/*tabs-4新建内容*/
	$(".build-new").click(function() {
		var $mainWord = $("<li><ul class='list-inline'><li><p>1.压缩的核心词：cap,cap;</p></li><li><p>&nbsp压缩强度&nbsp</p><input type='text' class='form-control intensity' />;</li><li><p>&nbsp强制提前的词：&nbsp</p><input type='text' class='form-control force-word' /></li></ul></li>");
		$(".main-ul").append($mainWord);
	});
	/*tabs-5添加属性分类*/
	$(".build-property").click(function() {
		var $property = $("<div class='product-property'><div class='product-property-head'><ul class='list-inline'><li><h5 class='property-type'>属性分类</h5></li><li><select><option>100%</option><option>高频</option><option>中频</option><option>低频</option><option>0%</option></select></li><li><span class='fa fa-close'></span></li><li><span class='fa fa-angle-left fa-lg'></span></li><li><span class='fa fa-angle-right fa-lg'></span></li></ul><input type='text' class='form-control property-name' /></div><div class='product-property-main'><h5>现有属性</h5><textarea class='form-control'></textarea><h5>创新属性</h5><textarea class='form-control'></textarea></div></div>");
//		$("title").html("123");
		$(".pro").append($property);
	});

})